$(function () {
    $("#save-banner-btn").click(function (event) {
        event.preventDefault();
        var self = $(this)
        var dialog = $("#bannerModel");
        var nameInput = $("input[name=name]");
        var imageInput = $("input[name=image_url]");
        var linkInput = $("input[name=link_url]");
        var priorityInput = $("input[name=priority]");

        var name = nameInput.val();
        var image_url = imageInput.val();
        var link_url = linkInput.val();
        var priority = priorityInput.val();
        var submitType = self.attr("data-type");
        var bannerId = self.attr("data-id");

        if (!name || !image_url || !link_url || !priority) {
            xtalert.alertErrorToast("请输入完整的信息")
            return;
        }
        if (submitType == "update") {
            var url = "/cms/ubanner/";
        } else {
            var url = "/cms/abanner/";
        }

        zlajax.post({
            "url": url,
            "data": {
                "name": name,
                "image_url": image_url,
                "link_url": link_url,
                "priority": priority,
                "banner_id": bannerId
            },
            "success": function (data) {
                if (data["code"] == 200) {
                    dialog.modal("hide")
                    window.location.reload()
                } else {
                    xtalert.alertErrorToast(data["message"])
                }
            }

        })
    })
})
$(function () {
    $("#add-banner-btn").click(function (event) {
            event.preventDefault();
            var dialog = $("#bannerModel");
            dialog.modal("show")
    })
})


$(function () {
    $(".save-banner-btn").click(function (event) {
        var self = $(this);
        event.preventDefault();

        var dialog = $("#bannerModel");
        dialog.modal("show");

        var tr = self.parent().parent();

        var name = tr.attr("data-name");
        var image_url = tr.attr("data-image");
        var link_url = tr.attr("data-link");
        var banner_id = tr.attr("data-id");
        var priority = tr.attr("data-priority")

        var nameInput = $("input[name=name]");
        var imageInput = $("input[name=image_url]");
        var linkInput = $("input[name=link_url]");
        var priorityInput = $("input[name=priority]");

        // 将属性绑定到 id是save-banner-btn的保存按钮上
        var saveBtn = dialog.find("#save-banner-btn")

        nameInput.val(name);
        imageInput.val(image_url);
        linkInput.val(link_url);
        priorityInput.val(priority);
        saveBtn.attr("data-type", "update");
        saveBtn.attr("data-id", banner_id);

    })
})


$(function () {
    $(".delete-banner-btn").click(function (event) {
        event.preventDefault();
        var self = $(this)
        var tr = self.parent().parent()
        var banner_id = tr.attr("data-id")

        xtalert.alertConfirm({
            "msg": "您确定删除这个轮播图吗？",
            "confirmCallback": function () {
                zlajax.post({
                    "url": "/cms/dbanner/",
                    "data": {
                        "banner_id": banner_id
                    },
                    "success": function f(data) {
                        if (data["code"] == 200) {
                            window.location.reload();
                        } else {
                            xtalert.alertErrorToast(data["message"])
                        }
                    }
                })
            }
        })
    })
})


$(function () {
    zlqiniu.setUp({
        'domain': 'http://q9ydebx6r.bkt.clouddn.com/',
        'browse_btn': 'upload-btn',
        'uptoken_url': '/common/uptoken/',
        'success': function (up, file, info) {
            var imageInput = $("input[name='image_url']");
            imageInput.val(file.name);
        }
    });
})